﻿using System;
using System.Windows.Forms;
using YNCNumberSys.db;
using cadApp= Autodesk.AutoCAD.ApplicationServices.Application;
namespace YNCNumberSys
{
    /// <summary>
    /// XSL 核查一遍，无事务20221121。
    /// 通过插入UserID，查找到UserEntity。
    /// </summary>
    public partial class UsmPassWordForm : Form
    {
        /// <summary>
        /// 用户的ID号，
        /// </summary>
        private int m_userID;
        public int UserID
        {
            get { return m_userID; }
            set { m_userID = value; }
        }

        UserEntity m_user;
        public UserEntity User
        {
            get { return m_user; }
            set { m_user = value; }
        }
        public UsmPassWordForm()
        {
            InitializeComponent();
        }

        private void PassWord_Load(object sender, EventArgs e)
        {
            userDal uDal = new userDal();
            m_user = uDal.GetUserByUserId(UserID);
            if(m_user!=null)
            {
                this.tbx_用户名.Text = m_user.Code;
                this.tbx_姓名.Text = m_user.Name;
            }
            else
            {
                cadApp.ShowAlertDialog("查无此用户！");
            }
        }

        private void btn_确定_Click(object sender, EventArgs e)
        {
            userDal uDal = new userDal();
            UserEntity user = uDal.GetUserByUserId(UserID);
            if(user.Pwd != EnOrDeEncryptHelper.EncryptText(tbx_旧密码.Text))
            {
                cadApp.ShowAlertDialog("【旧密码】验证失败，请重新输入！");
                return;
            }

            if (this.tbx_新密码.Text == tbx_旧密码.Text)
            {
                cadApp.ShowAlertDialog("【新密码】和【旧密码】不能相同，请重新输入！");
            }
            else
            {
                if (this.tbx_新密码.Text != this.tbx_确认密码.Text)
                {
                    cadApp.ShowAlertDialog("两次【新密码】不一致，请重新输入！");
                }
                else
                {
                    string sNewPwd = EnOrDeEncryptHelper.EncryptText(tbx_新密码.Text);
                    uDal.UpdatePwd(UserID,sNewPwd);
                    cadApp.ShowAlertDialog("用户密码已经修改！");
                    this.Hide();
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
        }

    }
}
